package com.ltedu.auth.vo;

import com.ltedu.common.contants.Constants;
import com.ltedu.common.domain.SecurityUser;
import com.ltedu.common.dto.UserDTO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName: LoginUser
 * @Auther: Jerry
 * @Date: 2020/11/11 14:39
 * @Desctiption: 登录用户信息
 * @Version: 1.0
 */
public class LoginUser implements UserDetails {
    /**
     * 用户信息
     */
    private SecurityUser user;

    /**权限数据*/
    private Collection<SimpleGrantedAuthority> authorities;

    public LoginUser(UserDTO userDTO) {
        this.user = userDTO.getUser();
        if (!CollectionUtils.isEmpty(userDTO.getRoles())) {
            this.authorities = new ArrayList<>();
            userDTO.getRoles().forEach(item -> authorities.add(new SimpleGrantedAuthority(item)));
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    @Override
    public String getUsername() {
        return this.user.getUsername();
    }

    public Long getUserId() { return this.user.getId(); }

    public String getMobile() { return this.user.getMobile(); }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Constants.NORMAL.equals(this.user.getState());
    }

}
